<?php

include_once "function_xml_basic.php";

$root_path = "/MINI/PROXY";

function GetXmlProxyServerInfoList()
{
	global $root_path;
	$query_string = $root_path. "/DELEGATE";
        $key_array = array("Enabled", "HTTP","FTP", "POP3",
			   "SMTP","IMAP","NNTP","LDAP","TELNET","DNS","SOCKS");
        return GetAttributList($query_string, $key_array);
}
/*
$ret_array = GetXmlProxyServerInfoList();
foreach ($ret_array as $i)
{
	print $i["MSN"];
	print $i["QQ"];
}
*/
function SetXmlProxyServerInfo($in_array)
{
        global $root_path;
        $query_string = $root_path. "/DELEGATE[1]";

        EditAllAttrOfNode($query_string, $in_array);

	ApplySysProxyConfig();
}

function FlushSysProxyConfig()
{
	$delegated = "/usr/local/sbin/delegated -Fkill -P";
	$IptablesFlush = "/usr/local/bin/iptables -F PROXYDELEGATED;";

	$command = "";
	$command .= $IptablesFlush;
	$command .= "/bin/kill -9 `/bin/pidof delegated` 1>/dev/null 2>&1;";
	
	/*$command .= $delegated. "6080 ;";
	$command .= $delegated. "6021 ;";
	$command .= $delegated. "6110 ;";
	$command .= $delegated. "6025 ;";
	$command .= $delegated. "6119 ;";
	$command .= $delegated. "6143 ;";
	$command .= $delegated. "6389 ;";
	$command .= $delegated. "6023 ;";
	$command .= $delegated. "6053 ;";
	$command .= $delegated. "6081 ;";
	*/
	$ret = shell_exec($command);
	//print $command;
	return $ret;
}

function ApplySysProxyConfig()
{
	FlushSysProxyConfig();
	
	ApplySysProxyConfig_One();
}
function ApplySysProxyConfig_One()
{
	$delegated = "/usr/local/sbin/delegated ";
	$iptables  = "/usr/local/bin/iptables -A PROXYDELEGATED  -p tcp --dport ";
	$action = " -j ACCEPT; ";
	$ret_array = GetXmlProxyServerInfoList();
	$ret = $ret_array[0];
	
	if ($ret['Enabled'] == 0) 
	{
		return ;
	}
	
	$command = ""; 
	if ($ret["HTTP"] == 1)
	{
		$command .= $delegated. " -P6080 SERVER=http ;";
		$command .= $iptables. " 6080 ". $action;
	}
	if ($ret["FTP"] == 1)
	{
		$command .= $delegated. " -P6021 SERVER=ftp ;";
		$command .= $iptables. " 6021 ". $action;
	}
	if ($ret["POP3"] == 1)
	{
		$command .= $delegated. " -P6110 SERVER=pop ;";
		$command .= $iptables. " 6110 ". $action;
	}
	if ($ret["SMTP"] == 1)
	{
		$command .= $delegated. " -P6025 SERVER=smtp ;";
		$command .= $iptables. " 6025 ". $action;
	}
	if ($ret["NNTP"] == 1)
	{
		$command .= $delegated. " -P6119 SERVER=nntp ;";
		$command .= $iptables. " 6119 ". $action;
	}
	if ($ret["IMAP"] == 1)
	{
		$command .= $delegated. " -P6143 SERVER=imap ;";
		$command .= $iptables. " 6143 ". $action;
	}
	if ($ret["LDAP"] == 1)
	{
		$command .= $delegated. " -P6389 SERVER=ldap ;";
		$command .= $iptables. " 6389 ". $action;
	}
	if ($ret["TELNET"] == 1)
	{
		$command .= $delegated. " -P6023 SERVER=telnet ;";
		$command .= $iptables. " 6023 ". $action;
	}
	if ($ret["DNS"] == 1)
	{
		$command .= $delegated. " -P6053 SERVER=dns ;";
		$command .= $iptables. " 6053 ". $action;
	}
	if ($ret["SOCKS"] == 1)
	{
		$command .= $delegated. " -P6081 SERVER=socks ;";
		$command .= $iptables. " 6081 ". $action;
	}
	//print $command;
	$ret = shell_exec ($command);
	return $ret;
}

?>
